
package com.lmx.client;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class BaseTester {
   // public static String CALL_URL = "http://192.168.1.106:9080/api";    //huibati
	public static String CALL_URL = "http://localhost:9080/api";
    // public static String CALL_URL =
    // "http://10.1.39.48:9082/sanguo3/business";

    private Integer uid = null;
    private String token = null;
    protected String method = "GET";


    private String action = null;
    private String reqString = null;
    protected String model = null;

    private String url = null;
    private String os = "Android";

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
    public BaseTester() {

    }

    public void baseTest() {
        this.url = CALL_URL + "/" + this.model;
        // url = "http://10.1.39.49:8085/SpringAccountJSONService";
        // url = "http://10.1.39.48:9080/shenliaoWebapp/json/lbsAction.action";
        // this.reqString =
        // "{ \"method\": \"getNear\", \"params\": [10.0,10.0,1],\"id\": 1}";
        System.out.println("url:" + this.url);
        System.out.println("reqString:" + reqString);
        String rtn = this.sendBody(this.url, this.reqString);
        System.out.println("rtn:" + rtn);
    }

    public String sendBody(String webAddress, String body) {
        HttpURLConnection httpConn = null;
        InputStream inStream = null;
        ByteArrayOutputStream bos = null;
        try {
            URL url = new URL(webAddress);
            httpConn = (HttpURLConnection) url.openConnection();
            System.out.println("openConnection over");
            httpConn.setReadTimeout(60 * 1000);
            httpConn.setRequestProperty(
                    "User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)");
            // httpConn.setConnectTimeout(3000);
            httpConn.setRequestProperty("Accept", "application/vnd.api+json");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
            httpConn.setRequestProperty("Accept-Language", "zh-CN");
            httpConn.setRequestProperty("Content-type", "application/vnd.api+json");
//            httpConn.setRequestProperty("os", this.os);
//            httpConn.setRequestProperty("ip", "192.168.16.18");
            //huibati
//            httpConn.setRequestProperty("tenantId", "1");
            if (this.uid != null) {
//                httpConn.setRequestProperty("uid", this.uid.toString());
//                httpConn.setRequestProperty("token", this.token);
            }
            // httpConn.setReadTimeout(6000);
            httpConn.setRequestMethod(method);
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false);
            OutputStream os = httpConn.getOutputStream();
            //huibati
            if(body!=null&&!"".equals(body)){
                os.write(body.getBytes());
            }
//            os.write(body.getBytes());
            os.flush();
            // for (int i=0; ; i++) {
            // String headerName = httpConn.getHeaderFieldKey(i);
            // String headerValue = httpConn.getHeaderField(i);
            // System.out.println("header name:" + headerName + "|header value:"
            // + headerValue);
            // if (headerName == null && headerValue == null) {
            // // No more headers
            // break;
            // }
            // if (headerName == null) {
            // // The header value contains the server's HTTP version
            // }
            // }
            inStream = httpConn.getInputStream();
            bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8000];
            int length = 0;
            while ((length = inStream.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (httpConn != null) {
                httpConn.disconnect();
            }
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (Exception e) {
                    System.out.println("inStream close err" + e);
                }
            }
        }
        System.out.println(bos.toByteArray().length + "字节");
        return new String(bos.toByteArray());
    }

    public static String sendBody(String webAddress, String guId, String gzId,
            String userid, String body, String dev)
            throws UnsupportedEncodingException {
        HttpURLConnection httpConn = null;
        InputStream inStream = null;
        ByteArrayOutputStream bos = null;
        try {
            URL url = new URL(webAddress);
            httpConn = (HttpURLConnection) url.openConnection();
            System.out.println("openConnection over");
            httpConn.setRequestProperty(
                    "User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)");
            // httpConn.setConnectTimeout(3000);
            httpConn.setRequestProperty("type", "outCall");
            httpConn.setRequestProperty("Accept", "*/*");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
            httpConn.setRequestProperty("Accept-Language", "zh-CN");
            httpConn.setRequestProperty("Content-type", "application/json-rpc");
            httpConn.setRequestProperty("Charsert", "UTF-8");
            httpConn.setRequestProperty("Accept-Charset", "UTF-8");
            httpConn.setRequestProperty("contentType", "UTF-8");
            httpConn.setRequestProperty("uid", userid);
            httpConn.setRequestProperty("gzId", gzId);
            httpConn.setRequestProperty("guId", guId);
            httpConn.setRequestProperty("dev", dev);
            httpConn.setRequestProperty("ip", "192.168.16.18");
            // httpConn.setReadTimeout(6000);
            httpConn.setRequestMethod("POST");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            httpConn.setUseCaches(false);

            PrintWriter out = new PrintWriter(new OutputStreamWriter(
                    httpConn.getOutputStream(), "utf-8"));
            out.println(body);
            out.close();

            inStream = httpConn.getInputStream();
            bos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8000];
            int length = 0;
            while ((length = inStream.read(buffer)) != -1) {
                bos.write(buffer, 0, length);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (httpConn != null) {
                httpConn.disconnect();
            }
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (Exception e) {
                    System.out.println("inStream close err" + e);
                }
            }
        }
        String result = new String(bos.toByteArray(), "UTF-8");
        return result;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getReqString() {
        return reqString;
    }

    public void setReqString(String reqString) {
        this.reqString = reqString;
    }
    
    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }
}
